<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Member;
use App\Models\OperateLog;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;


class RegisterController extends Controller
{
    use RegistersUsers;

    public function showRegistrationForm()
    {
        return view('home.auth.register');
    }

    public function redirectPath()
    {
        return '/';
    }

    protected function guard()
    {
        return Auth::guard('member');
    }

    protected function validator(array $data)
    {
        $messages = [
            "email.required" => '邮箱不能为空',
            'email.regex' => '邮箱格式错误',
            'email.unique' => '邮箱已使用',

            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '验证码错误',
            'password.required' => '密码不能为空',
            'password.min' => '密码最短为6个字符',
            'password.confirmed' => '密码最短为6个字符',
        ];

        return Validator::make($data, [
            'email' => [
                'required',
                'regex:/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/',
                'unique:members'],
            'password' => ['required', 'confirmed', 'min:6'],
            'captcha' => ['required', 'captcha'],
        ], $messages);
    }

    protected function create(array $data)
    {
        return Member::create([
            'email' => $data['email'],
            'name' => $data['name'],
            'password' => $data['password']
        ]);
    }

    protected function registered(Request $request, $user)
    {
        OperateLog::create([
            'user_id' => $user->id,
            'uri' => $request->url(),
            'parameter' => json_encode($request->all()),
            'method' => $request->method()
        ]);
    }

}
